package cn.car.controller;

import cn.car.biz.SysPermissionBiz;
import cn.car.domains.vo.BasePage;
import cn.car.domains.vo.Result;
import cn.car.domains.vo.sysPermission.CreatePermissionReq;
import cn.car.domains.vo.sysPermission.PermissionListReq;
import cn.car.domains.vo.sysPermission.SysPermissionRes;
import cn.car.domains.vo.sysPermission.UpdatePermissionReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/permission")
@Tag(name = "sysPermissionController", description = "权限管理相关接口")
public class SysPermissionController {

    @Resource
    private SysPermissionBiz sysPermissionBiz;

    /**
     * 创建权限
     */
    @PostMapping("/create")
    @Operation(description = "创建权限接口：根据请求参数创建一个新的权限")
    public Result<Void> createPermission(@RequestBody @Valid CreatePermissionReq req) {
        sysPermissionBiz.createPermission(req);
        return Result.success();
    }

    /**
     * 根据ID获取权限信息
     */
    @GetMapping("/getById")
    @Operation(description = "根据权限ID查询权限信息，返回权限详情")
    public Result<SysPermissionRes> getPermissionById(@Parameter(schema = @Schema(name = "id", description = "权限id")) @RequestParam Long id) {
        return Result.success(sysPermissionBiz.getPermissionById(id));
    }

    /**
     * 根据权限名获取权限信息
     */
    @GetMapping("/getByCode")
    @Operation(description = "根据权限code查询权限信息，返回权限详情")
    public Result<SysPermissionRes> getPermissionByCode(@Parameter(schema = @Schema(name = "permissionName", description = "权限名")) @RequestParam String permissionCode) {
        return Result.success(sysPermissionBiz.getPermissionByCode(permissionCode));
    }

    /**
     * 更新权限信息
     */
    @PostMapping("/update")
    @Operation(description = "更新权限信息接口")
    public Result<Void> updatePermission(@RequestBody @Valid UpdatePermissionReq req) {
        sysPermissionBiz.updatePermission(req);
        return Result.success();
    }

    /**
     * 删除权限
     */
    @PostMapping("/delete")
    @Operation(description = "删除权限接口：根据ID删除权限")
    public Result<Void> deletePermission(@Parameter(schema = @Schema(name = "id", description = "权限id")) @RequestParam Long id) {
        sysPermissionBiz.deletePermission(id);
        return Result.success();
    }

    /**
     * 分页查询权限信息
     */
    @PostMapping("/list")
    @Operation(description = "分页查询权限信息接口")
    public Result<BasePage<SysPermissionRes>> list(@RequestBody PermissionListReq query) {
        return Result.success(sysPermissionBiz.list(query));
    }
}

